import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Solution1765 {
    public int[][] highestPeak(int[][] isWater) {
        int[] dx=new int[]{1,0,-1,0};
        int[] dy=new int[]{0,1,0,-1};
        int m=isWater.length,n=isWater[0].length;
        int[][] ans=new int[m][n];
        Queue<int[]> queue=new LinkedList<>();
        for (int i=0;i<m;i++){
            for (int j=0;j<n;j++){
                if (isWater[i][j]==0){
                    ans[i][j]=Integer.MIN_VALUE;
                }else{
                    queue.offer(new int[]{i,j});
                }
            }
        }
        while (!queue.isEmpty()){
            int[] t=queue.poll();
            for (int i=0;i<4;i++){
                int xx=t[0]+dx[i];
                int yy=t[1]+dy[i];
                if (valid(xx,yy,m,n) && ans[xx][yy]==Integer.MIN_VALUE){
                    ans[xx][yy]=ans[t[0]][t[1]]+1;
                    queue.offer(new int[]{xx,yy});
                }
            }
        }
        return ans;
    }

    boolean valid(int x,int y,int m,int n){
        return x>=0 && x<m && y>=0 && y<n;
    }

    public static void main(String[] args) {
        int[][] ans=new Solution1765().highestPeak(new int[][]{{0,0,1},{1,0,0},{0,0,0}});
        for (int[] an : ans) {
            System.out.println(Arrays.toString(an));
        }
    }
}
